<?php
namespace Home\Controller;
use Think\Controller;
class GiftController extends BaseController {
    public function index(){
        $Game = M('Game');
        $map = array('hot'=>1,'status'=>1);
        $hot_list = $Game->where($map)->order('order_number desc,add_time desc')->select();
        $banner = array();
        if (!empty($hot_list)) {
            foreach ($hot_list as $item) {
                $data = array(
                    'title' => $item['name'],
                    'subtitle' => $item['sub_title'],
                    'img'=>'../appc/Uploads/'.$item['s_pic'],
                    'url' => U('Index/detail', array('id'=>$item['id']))
                );
                $banner[] = $data;
            }
        }
        $this->assign('banner',$banner);
        $this->display();
    }
    
    public function giftList(){
        $map = array('status'=>1);
        $game_id = I('post.game_id');
        if(!empty($game_id)){
            $map['game_id'] = $game_id;
        }
        $page = I('page',1);

        $pagesize = I('pagesize', 20);
        $list = D('PlayerGift')->where($map)->order('order_number desc,add_time desc')->limit(($page-1)*$pagesize, $pagesize)->select();
        $rst = array();
        if (!empty($list)) {
            foreach ($list as $item){
                $data = array(
                    'gift' => $item['id'],
                    'title' => $item['title'],
                    'subtitle' => $item['sub_title'],
                    'img'=>'../appc/Uploads/'.$item['pic'],
                    'percent'=> $item['total_num'] == 0 ?0:floor(($item['total_num'] - $item['get_num']) / $item['total_num'] * 100)
                );
                $rst[] = $data;
            }
        }
        $this->ajaxReturn($rst);
    }
    
    public function getCode(){
        $user_id = session('user_id');

        $is_attention = empty(session('is_attention'))?0:session('is_attention');
        $openid = session('openid');
        import('@.Lib.Wx');
        $wx = new \Boo\WX(array('get' => 'userinfo'));
        $subscribe = $wx->get_user_info($openid );
        $subscribe = empty($subscribe)?0:$subscribe;
        if($is_attention == 1 && $subscribe == 0){
            $this->ajaxReturn(array('status'=>'2','info'=>'关注公众号，领取更多礼包'));
        }
        
        $gift_id = I('gift');
        if (empty($gift_id) || !is_numeric($gift_id)) {$this->error('sb');}
        $GiftCode = D('PlayerGiftCode');
        // 先查询是否领过
        $map = array('gift_id'=>$gift_id,'player_id'=>$user_id);
        $one = $GiftCode->where($map)->find();
        if (!empty($one)) {
            $this->success($one['code']); return;
        }
        
        $Gift = D ('PlayerGift');
        $map = array('id'=>$gift_id);
        $gift = $Gift->where($map)->find();
        if (empty($gift)) {$this->error('Error');}
        if ($gift['total_num'] - $gift['get_num'] <=0) {$this->error('游戏太火爆了，领完了。');}
        $Gift->where($map)->setInc('get_num');
        
        // 去兑换码表里，找兑换码
        $map = array('gift_id'=>$gift_id,'player_id'=>0);
        $one = $GiftCode->where($map)->find();
        if (!empty($one)) {
            $data = array('id'=>$one['id'],'player_id'=>$user_id, 'get_time'=>getFomartDate());
            if (false === $GiftCode->save($data)) {$this->error('获取失败');}
            $this->success($one['code']);
        } else {
            $this->error('游戏太火爆了，领完了');
        }
        
    }
    
}